//apicloud app打开的远程wap页面，封装进的apicloud功能
//示例： pages/taxReward里面的几个页面
//app打开远程页面注意 ： 在router里面关闭对应的页面登录验证
let apiReady = {
  isReady: false, //有可能进入页面，api对象已经存在，判断是否存在
  ready: function () {
    let thiz = this;
    function setApi() {
      //所有关于apicloud的系统方法，都要放在setApi里面，来初始化  thiz指向apiReady。
      thiz.isReady = true;
      thiz.sd = api.fsDir;
      // 项目目录路径
      thiz.rd = api.wgtRootDir;
      thiz.deviceModel = api.deviceModel; //手机型号
      thiz.hd = api.wgtRootDir + "/html/";// 页面根路径
      thiz.inAV = "6.1.0"; //接口版本 ， 必须跟app内部保持一致
      thiz.parse = function (json) {
        let result = null;
        if (json && typeof json == "string") {
          result = JSON.parse(json);
        } else {
          result = json;
        }
        return result;
      };
      thiz.log = function (mark, logtext) {
        var mk = "";
        if (mark) {
          mk = mark;
        }
        if (logtext instanceof Array || logtext instanceof Object) {
          logtext = JSON.stringify(logtext);
        }
        console.log(mk + "----  " + logtext);
      };
      thiz.getSyncStorage = function (key) {
        if (key) {
          let value = api.getPrefs({sync: true, key: key});
          return value;
        } else {
          return null;
        }
      };
      thiz.setStorage = function (key, value) {
        var val = value;
        if (key) {
          if (val instanceof Object || val instanceof Array) {
            val = JSON.stringify(val);
          }
          api.setPrefs({
            key: key,
            value: val
          });
        }
      };
      thiz.getAccount = function () {
        let accountStr = thiz.parse(thiz.getSyncStorage("account"));
        if (accountStr.userLogin == "") return false;
        let accturn = thiz.parse(accountStr.userLogin);
        return accturn
      };
      thiz.trigger = function (event, data) {
        if (event) {
          api.sendEvent({
            name: event,
            extra: data
          });
        }
      };
      thiz.listen = function (event, callback) {
        if (event && typeof event == "string") {
          api.addEventListener({
            name: event
          }, callback);
        }
        if (event instanceof Array) {
          for (var i = 0; i < event.length; i++) {
            var e = event[i];
            api.addEventListener({
              name: e
            }, callback);
          }
        }
      };
      thiz.toWin = function (name) {
        api.closeToWin({
          name: name
        });
        thiz.setStorage("curWin", name);
      };
      thiz.openWin = function (config) {
        if (config instanceof Object) {
          // 保存当前窗口名称到本地数据库
          thiz.setStorage("curWin", config.name);
          api.openWin(config);
        }
      };
      thiz.closeW = function (name) {
        if (!name) {
          api.closeWin();
        } else {
          api.closeWin({
            name: name
          });
        }
        thiz.setStorage("curWin", "");
        thiz.setStorage("leftW", name);
      };
      thiz.closeFlower = function (time) {
        // 延时关闭
        setTimeout(function () {
          api.closeFrame({
            name: "flower"
          });
        }, time ? time : 1000);
      };
      thiz.toBottom = function (callback) {
        api.addEventListener({
          name: "scrolltobottom",
          extra: {
            threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
          }
        }, function (ret, err) {
          callback(ret, err);
        });
      };
    }
    if (!thiz.isReady) {
      setApi();
    }
    return apiReady;
  }
};
export {apiReady};
//抓取整个export出来的apiReady对象，不能直接使用，要调用apiReady.ready()来初始化一下
